﻿using System;
using System.Collections;
using UnityEngine;
using Random = System.Random;

namespace GameFramework.skill.deployer.details
{
    public class BulletSkillDeployer:SkillDeployer
    {
        public override void DeploySkill()
        {
            selectTarges();
            if (_skill.targets.Count == 0)
            {
                Destroy(gameObject);
            }

            //向目标移动
            StartCoroutine(moveToTargetBesier());
        }

        private IEnumerator moveToTargetBesier()
        {
            Transform target = _skill.targets[0];
            //target的碰撞检测范围或者模型范围
            float colliderRange = 0.5f;
            float t = 0;
            //选取控制点
            Vector3 direction = target.position-transform.position;
            //垂直方向
            Vector2 perpendicular = Vector2.Perpendicular(direction).normalized;
            Random random = new Random();
            int next = random.Next(-1,1);
            if (next == 0)
            {
                next = 1;
            }

            int offset = 0;
            // var skillConfigBezierCtlOffset = _skill.skillConfig.bezierCtlOffset;
            // if (skillConfigBezierCtlOffset != null || skillConfigBezierCtlOffset.Length != 0)
            // {
            //     offset = random.Next(skillConfigBezierCtlOffset[0], skillConfigBezierCtlOffset[1]);
            // }

            //垂直方向的偏移量
            perpendicular = (perpendicular * next).normalized * offset;
            direction  = (direction+new Vector3(perpendicular.x,perpendicular.y))*0.25f;
            Vector3 ctlPoint = transform.position + direction;
            Vector3 p1 = transform.position;
            Vector3 p2 = target.position;
            // perpendicular 
            //曲线运动 2阶模拟追踪子弹  3阶或更高 模拟随机轨迹
            while(Vector3.Distance(target.position, transform.position) > colliderRange)//表示还未击中目标
            {
                yield return null;
                //二阶：B(t) = P1*(1-t)^2+2*P2*t(1-t)+P3*t^2

                Vector3 newPos = p1 * (float)Math.Pow(1 - t, 2) + 2 * ctlPoint * t * (1 - t) + p2 * (float)Math.Pow(t, 2);
                transform.position = newPos;
                // t += Time.deltaTime * _skill.skillConfig.skillPrefabMoveSpeed;
            }
            //击中目标,播放击中特效，同时执行效果
            //TODO 播放击中特效
            effectTargets();
            Destroy(gameObject);
        }

        private IEnumerator moveToTargetDirect()
        {
            Transform target = _skill.targets[0];
            //target的碰撞检测范围或者模型范围
            float colliderRange = 0.5f;
            //直线
            while(Vector3.Distance(target.position, transform.position) > colliderRange)//表示还未击中目标
            {
                yield return null;

                Vector3 direction = (target.position-transform.position).normalized;
                // transform.Translate(direction*Time.deltaTime*_skill.skillConfig.skillPrefabMoveSpeed);
            }
            //击中目标,播放击中特效，同时执行效果
            //TODO 播放击中特效
            effectTargets();
            Destroy(gameObject);
        }
    }
}